package xmsg

import "gitee.com/shuohe/xutils"

type IFieldValidator interface {
	Validate(data *Variant) bool
}

//-----------------------------------------------------------------------------
type RangeValidator struct {
	Min int
	Max int
}

func (this *RangeValidator) Validate(data *Variant) bool {
	i := data.AsInt()
	return this.Min <= i && i <= this.Max
}

//-----------------------------------------------------------------------------
type GreaterEqualValidator struct {
	Base int
}

func (this *GreaterEqualValidator) Validate(data *Variant) bool {
	i := data.AsInt()
	return i >= this.Base
}

//-----------------------------------------------------------------------------
type DigitValidator struct{}

func (this *DigitValidator) Validate(data *Variant) bool {
	bs := data.AsBytes()
	for _, b := range bs {
		if !xutils.IsDigit(b) {
			return false
		}
	}
	return true
}

//-----------------------------------------------------------------------------
type AlphaDigitValidator struct{}

func (this *AlphaDigitValidator) Validate(data *Variant) bool {
	bs := data.AsBytes()
	for _, b := range bs {
		if !xutils.IsAlphaDigit(b) {
			return false
		}
	}
	return true
}

//-----------------------------------------------------------------------------
type PrintableValidator struct{}

func (this *PrintableValidator) Validate(data *Variant) bool {
	bs := data.AsBytes()
	for _, b := range bs {
		if !xutils.IsPrintable(b) {
			return false
		}
	}

	return true
}
